package itheima.zuoye.Test3;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要查找的数字：");
        double key = scanner.nextDouble();
        double[] arr = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.1};

         int index  =binarySearch(arr,key);
         if (index == -1) {
             System.out.println("没有找到");
         } else {
             System.out.println("找到了，下标为：" + index);
         }


    }
    public static int binarySearch(double[] a, double key) {
        if (a == null|| a.length == 0){
            return -1;
        }
        int left = 0;
        int right = a.length - 1;
        while (left <= right) {
            int mid = (left + right) >> 1;
            if (a[mid] == key) {
                return  mid;
            } else if (a[mid] > key) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return -1;
    }
}
